package cn.stylefeng.guns.modular.manager.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.ArchiveBorrow;
import cn.stylefeng.guns.modular.manager.entity.ArchiveBorrowDetail;
import cn.stylefeng.guns.modular.manager.mapper.ArchiveBorrowDetailMapper;
import cn.stylefeng.guns.modular.manager.model.params.ArchiveBorrowDetailParam;
import cn.stylefeng.guns.modular.manager.model.result.ArchiveBorrowDetailResult;
import cn.stylefeng.guns.modular.manager.service.ArchiveBorrowDetailService;
import cn.stylefeng.guns.modular.manager.service.ArchiveBorrowService;
import cn.stylefeng.guns.sys.core.constant.state.YesNo;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 档案借阅信息明细表 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-23
 */
@Service
public class ArchiveBorrowDetailServiceImpl extends ServiceImpl<ArchiveBorrowDetailMapper, ArchiveBorrowDetail> implements ArchiveBorrowDetailService {

    @Override
    public void add(ArchiveBorrowDetailParam param) {
        ArchiveBorrowDetail entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional
    public void delete(ArchiveBorrowDetailParam param) {
        final ArchiveBorrowDetail byId = this.getById(getKey(param));
        if (byId != null && byId.getGhsj() == null) {
            this.removeById(getKey(param));
        }else {
            throw new RuntimeException("无法删除已归还的借阅档案");
        }
        this.checkAllReturn(byId.getDajyid());
    }

    @Override
    public void update(ArchiveBorrowDetailParam param) {
        ArchiveBorrowDetail oldEntity = getOldEntity(param);
        ArchiveBorrowDetail newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveBorrowDetailResult findBySpec(ArchiveBorrowDetailParam param) {
        return null;
    }

    @Override
    public List<ArchiveBorrowDetailResult> findListBySpec(ArchiveBorrowDetailParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveBorrowDetailParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveBorrowDetailParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveBorrowDetail getOldEntity(ArchiveBorrowDetailParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveBorrowDetail getEntity(ArchiveBorrowDetailParam param) {
        ArchiveBorrowDetail entity = new ArchiveBorrowDetail();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Autowired
    private ArchiveBorrowService archiveBorrowService;

    @Override
    @Transactional
    public void batReturn(String[] ids) {
        final Collection<ArchiveBorrowDetail> archiveBorrowDetails = listByIds(Arrays.asList(ids));
        final List<ArchiveBorrowDetail> collect = archiveBorrowDetails.parallelStream()
                .filter(it -> it.getGhsj() == null)
                .peek(it -> it.setGhsj(new Date()))
                .collect(Collectors.toList());
        if (collect.isEmpty()) return;
        //借阅关联表都保存上归还时间
        this.updateBatchById(collect);

        //检查是不是全部都归还了
        final Long dajyid = collect.get(0).getDajyid();
        checkAllReturn(dajyid);
    }

    private void checkAllReturn(Long dajyid) {
        //查询关联表里面，归还时间为null 大于0代表有没还的
        int count = this.count(Wrappers.<ArchiveBorrowDetail>lambdaQuery().and(it -> {
            it.eq(ArchiveBorrowDetail::getDajyid, dajyid);
            it.isNull(ArchiveBorrowDetail::getGhsj);
            return it;
        }));

        if (count == 0) {
            final ArchiveBorrow byId = this.archiveBorrowService.getById(dajyid);
            byId.setSfgh(YesNo.YES.getCode());
            this.archiveBorrowService.updateById(byId);
        }
    }
}
